<?php
namespace Bshop\Controller;
use Think\Controller;
class PublicController extends Controller{
	public function verify(){
		$Verify = new \Think\Verify();
		$Verify->length   = 4;
		$Verify->codeSet = '0123456789';
		$Verify->entry();
	}
	public function upload(){
		$type =  intval($_GET['type']);
		$page = intval($_GET['page']);
		$width = intval($_GET['width']);
		$height = intval($_GET['height']);
		$path = $this->path($type);
		$upload = new \Think\Upload();
		$upload->maxSize = C('UPLOAD_MAX_SIZE');
		$upload->rootPath = $path['true_path'];
		$upload->saveName = array('uniqid','');
		$upload->exts     = array('jpg', 'gif', 'png', 'jpeg');
		$upload->autoSub  = true;
		$upload->subName  = array('date','Ymd');
		$info   =   $upload->upload();
		if(!$info) {// 上传错误提示错误信息
			$data['code'] = 300;
			$data['msg'] = $upload->getError();
		}else{// 上传成功 获取上传文件信息
			$url = $path['true_path'].date("Ymd")."/".$info['file']['savename'];
			if(!empty($width) && !empty($height)){
				$a = getimagesize($url);
				$b = $width/$height;
				if($a[0]/$a[1]!=$b){
					unlink($url);
					$data['code'] = 300;
					$data['msg'] = "请上传图片的尺寸为".$width."*".$height;
					echo json_encode($data);exit;
				}
			}
			$data['code'] = 200;
			$data['msg'] = $path['false_path'].date("Ymd")."/".$info['file']['savename'];
			if(!empty($page)){
			   $data['page'] = $page;
			}	
		}
		echo json_encode($data);
	}
	/**
	 * 发送邮件
	 * @param unknown $type
	 * @return string
	 */
	public function new_email_send($title,$c){
		//$path = cachepath::path(2);
		//$headinfo = F('web_set','',$path);
		$host1="smtp.exmail.qq.com";
		$username="hi@shenzhenapp.com";
		$pass="Zz123456";
		$email ="504452335@qq.com";
		Vendor("PHPMailer.class#phpmailer");    //在这里要用#转义到class.phpmailer中的 ' . '符号
		$mail = new PHPMailer();
		$mail->IsSMTP();                                       // set mailer to use SMTP
		$mail->Host = "$host1";     // specify main and backup server
		$mail->SMTPAuth = true;            // turn on SMTP authentication开启身份验证
		$mail->Username = "$username";        // SMTP username服务器用户名
		$mail->Password = "$pass";        // SMTP password服务器用户密码
		$mail->From = "$username";      //发件人E-mail
		$mail->FromName = "贷了网服务中心";      //发件人名称
		$mail->AddAddress("$email",""); //收件人E-mail和称呼
		$mail->WordWrap = 50;                                  // set word wrap to 50 characters
		$mail->IsHTML(true);                                   // set email format to HTML
		$mail->Subject = "$title";    //邮件标题
		$mail->Body    = "$c";
		$mail->CharSet='utf-8';         //定义编码格式,否则可能乱码
		if(!$mail->Send())           //发送邮件
		{
			$result=0;
		}else{
			$result=1;
	
		}
		return $result;
	}
	private function path($type){
		switch ($type){
			case 1://
				$path['true_path'] = "./Public/Uploads/web/";
				$path['false_path'] = "/Public/Uploads/web/";
			break;
			case 2://专题图片
				$path['true_path'] = "./Public/Uploads/subject/";
				$path['false_path'] = "/Public/Uploads/subject/";
			break;
			case 3://商家图片
				$path['true_path'] = "./Public/Uploads/shop/";
				$path['false_path'] = "/Public/Uploads/shop/";
			break;
			case 4://会员图片
				$path['true_path'] = "./Public/Uploads/user/";
				$path['false_path'] = "/Public/Uploads/user/";
			break;
			case 5://信息推送图片
				$path['true_path'] = "./Public/Uploads/information/";
				$path['false_path'] = "/Public/Uploads/information/";
			break;
			case 6://活动图片
				$path['true_path'] = "./Public/Uploads/activity/";
				$path['false_path'] = "http://".$_SERVER['HTTP_HOST']."/Public/Uploads/activity/";
			break;
			case 7://广告图片
				$path['true_path'] = "./Public/Uploads/ad/";
				$path['false_path'] = "/Public/Uploads/ad/";
			break;
			case 8://咨询图片
				$path['true_path'] = "./Public/Uploads/msg/";
				$path['false_path'] = "/Public/Uploads/msg/";
			break;
			case 9://活动分类图片
				$path['true_path'] = "./Public/Uploads/activityclass/";
				$path['false_path'] = "/Public/Uploads/activityclass/";
			break;
			default:
				$path['true_path'] = "./Public/Uploads/web/";
				$path['false_path'] = "/Public/Uploads/web/";
			break;
		}
		return $path;
	}
}